package com.project.erp.action.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.project.erp.action.BaseAction;
import com.project.erp.common.utils.Page;
import com.project.erp.model.system.Privilege;
import com.project.erp.model.system.Role;
import com.project.erp.service.system.RoleService;

@Controller
@Scope("prototype")
public class RoleAction extends BaseAction implements ModelDriven<Role>{

	private static final long serialVersionUID = 8648117880606418547L;

	@Autowired
	private RoleService roleService;
	
	private Role role;
	
	private List<Privilege> privileges;
	
	private String roleIds;
	
	private String privilegeIds;
	
	@Override
	public String execute() throws Exception {
		logger.info("entry listRole method ...");
		return SUCCESS;
	}
	
	/**
	 * 角色列表信息查询
	 * @return success
	 * @throws Exception exception
	 */
	public String getRoleListData() throws Exception{
		logger.debug("entry getRoleListData method ...");
		try{
			Page<Role> page = new Page<Role>();
			page.setPageNo(getPage());
			page.setPageSize(getRows());
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
			page = roleService.getRolePageBySearch(page, role);
			//设置表格返回数据
			this.getResultMap().put("total", page.getTotalCount());
			this.getResultMap().put("rows", page.getResult());
		}catch(Exception e){
			logger.error("execute getRoleListData error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 到角色添加页面
	 * @return
	 * @throws Exception
	 */
	public String toAddRole() throws Exception{
		return SUCCESS;
	}
	
	/**
	 * 保存角色信息
	 * @return success
	 * @throws Exception exception
	 */
	public String addRole() throws Exception{
		logger.debug("entry addRole method!...");
		try{
			roleService.saveRole(role);
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			this.getResultMap().put("msg","添加角色信息成功");
		}catch(Exception e){
			//返回错误信息
			logger.error("execute addRole page error!", e);
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg","添加角色信息失败");
		}
		return SUCCESS;
	}
	
	/**
	 * 修改角色信息
	 * @return
	 * @throws Exception
	 */
	public String toEditRole() throws Exception{
		logger.debug("entry toEditRole method!...");
		try{
			Long roleId = role.getId();
			role = roleService.getRoleById(roleId);
		}catch(Exception e){
			logger.error("execute toEditRole page error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 分配权限信息
	 * @return
	 * @throws Exception
	 */
	public String toPrivilegeForRole() throws Exception{
		return SUCCESS;
	}
	
	/**
	 * 修改角色信息
	 * @return success
	 * @throws Exception exception
	 */
	public String editRole() throws Exception{
		logger.debug("entry editRole method!...");
		try{
			Role persistentRole = roleService.getRoleById(role.getId());
			
			role.setPrivileges(persistentRole.getPrivileges());
			//注:创建日期在这拷贝为空覆盖以前的,所有不拷贝创建日期
			BeanUtils.copyProperties(role, persistentRole, new String[]{});
			//修改用户信息
			roleService.editRole(persistentRole);
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			this.getResultMap().put("msg","修改角色信息成功");
		}catch(Exception e){
			//返回错误信息
			logger.error("execute editRole page error!", e);
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg","修改角色信息失败");
		}
		return SUCCESS;
	}
	
	/**
	 * 删除角色信息
	 * @return
	 * @throws Exception
	 */
	public String delRole() throws Exception{
		logger.debug("entry delRole method!...");
		try{
			logger.info("roleIds="+roleIds);
			String[] arrayIds = roleIds.split(",");
			boolean deleteFlag = roleService.delRole(arrayIds);
			//返回成功信息
			if(deleteFlag){
				this.getResultMap().put("result", SUCCESS);
			}else{
				this.getResultMap().put("errorCode","10001");
			}
		}catch(Exception e){
			//返回错误信息
			logger.error("删除角色信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
		}
		return SUCCESS;
	}
	
	/**
	 * 分配角色权限方法
	 * @return success
	 * @throws Exception exception
	 */
	public String privilegeForRole() throws Exception{
		logger.debug("entry privilegeForRole method!...");
		try{
			List<Privilege> rolePrivileges = new ArrayList<Privilege>();
			if(StringUtils.isNotBlank(privilegeIds)){
				String[] arrayIds = privilegeIds.split(",");
				for (String str : arrayIds) {
					Privilege p = new Privilege();
					p.setId(Long.valueOf(str));
					rolePrivileges.add(p);
				}
			}
			Role r = roleService.getRoleById(role.getId());
			r.setPrivileges(rolePrivileges);
			roleService.editRole(r);
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			this.getResultMap().put("msg", "分配权限信息成功");
		}catch(Exception e){
			//返回错误信息
			logger.error("privilegeForRole method error!",e);
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg", "分配权限信息失败");
		}
		return SUCCESS;
	}
	
	public List<Privilege> getPrivileges() {
		return privileges;
	}

	public void setPrivileges(List<Privilege> privileges) {
		this.privileges = privileges;
	}

	@Override
	public Role getModel() {
		if(null == role){
			role = new Role();
		}
		return role;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public String getPrivilegeIds() {
		return privilegeIds;
	}

	public void setPrivilegeIds(String privilegeIds) {
		this.privilegeIds = privilegeIds;
	}
	
}
